﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Phoenix.Xna.Framework.Reflection
{
    /// <summary>
    /// Extends the functionality of System.Reflection.MemberInfo instances.
    /// </summary>
    public static class MemberInfoExtensions
    {
        /// <summary>
        /// Get all attributes marked on this MemberInfo of a specific type.
        /// </summary>
        /// <typeparam name="A">The type of attributes to find.</typeparam>
        /// <param name="memberInfo">The extended object.</param>
        /// <param name="inherit">Specifies whether to search this member's inheritance chain to find attributes.</param>
        /// <returns>The first attribute of the specified type.</returns>
        public static A GetFirstAttributeOfType<A>(this MemberInfo memberInfo, bool inherit)
            where A : Attribute
        {
            return memberInfo.GetAllAttributeOfType<A>(inherit).FirstOrDefault();
        }

        /// <summary>
        /// Get all attributes marked on this MemberInfo of a specific type.
        /// </summary>
        /// <typeparam name="A">The type of attributes to find.</typeparam>
        /// <param name="assembly">The extended object.</param>
        /// <param name="inherit">Specifies whether to search this member's inheritance chain to find attributes.</param>
        /// <returns>A list of attributes of the specified type.</returns>
        public static List<A> GetAllAttributeOfType<A>(this MemberInfo memberInfo, bool inherit)
           where A : Attribute
        {
            return memberInfo.GetCustomAttributes(typeof(A), inherit)
                .OfType<A>()
                .ToList();
        }
    }
}
